package healththink
import grails.plugins.springsecurity.*

class ForumController {
    def springSecurityService
  
    @Secured(['ROLE_OWNER', 'ROLE_MODERATOR', 'IS_AUTHENTICATED_FULLY'])
    def scaffold = true
    
    def mobileService
    
    def afterInterceptor = [action: this.&handleMobile]
	 
    private handleMobile(model, modelAndView) {
            if (modelAndView && mobileService.detect(request)) {
                modelAndView.viewName = '/mobile/' + modelAndView.viewName          
            }
    }
    
    @Secured(['IS_AUTHENTICATED_ANONYMOUSLY'])
    def index() { 
        redirect(action: "list")
    }
    
    /*
    def beforeInterceptor = [action:this.&checkUser,except:
        ['index','list']]
    */
    @Secured(['IS_AUTHENTICATED_ANONYMOUSLY'])
    def checkUser(){
        redirect(controller:'user',action:'login')
    }
    
    @Secured(['ROLE_OWNER', 'ROLE_CONTRIBUTOR', 'ROLE_MODERATOR', 'IS_AUTHENTICATED_FULLY'])
    def createNew = {
        if(!params.create){
            def facility = Facility.findByName(params.name)
            [facility: facility]
        }else{
            def user = User.get(springSecurityService.principal.id)
            def facility = Facility.findByName(params.facility.name)
            def forum = new Forum(params)
            def post = new Post(params)
            forum.addToPosts(post)
            user.addToForums(forum)
            post.user = user
            user.addToPosts(post)
            facility.addToForums(forum)
            if(mobileService.detect(request)){
                forward controller: "facility", action: "view", params: [name: facility.name]
            }else{
                redirect(controller: "facility", action: "view", params: [name: facility.name])
            }
        }
    }
    
  @Secured(['IS_AUTHENTICATED_ANONYMOUSLY'])
    def view = {
        if(!params.addPost){
            def facility = Facility.findByName(params.facilityName)
            def forum = Forum.findByFacilityAndTopic(facility, params.topic)
            [facility: facility, forum: forum]
        }else{
            def user = User.get(springSecurityService.principal.id)
            def facility = Facility.findByName(params.facility.name)
            def forum = Forum.findByFacilityAndTopic(facility, params.topic)
            def post = new Post(params)
            post.user = user
            user.addToPosts(post)
            forum.datePosted = new Date()
            forum.addToPosts(post)
            if(mobileService.detect(request)){
                forward controller: "facility", action: "view", params: [name: facility.name]
            }else{
                redirect(controller: "facility", action: "view", params: [name: facility.name])
            }
        }
    }
    
    def deletePost = {
        def user = User.get(springSecurityService.principal.id)
        def post = Post.get(params.postId)
        def facility = Facility.findByName(params.facilityName)
        def forum = Forum.findByFacilityAndTopic(facility, params.topic)
        user.removeFromPosts(post)
        forum.removeFromPosts(post)
        post.delete()
        if(mobileService.detect(request)){
            forward action: "view", params: [facilityName: params.facilityName, topic: params.topic]
        }else{
            redirect(action: "view", params: [facilityName: params.facilityName, topic: params.topic])
        }
    }
}
